package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.WarehouseDetailVo;
import com.woniuxy.oldmanapi.cvo.WarehouseVo;
import com.woniuxy.oldmanapi.entity.Warehouse;
import com.woniuxy.oldmanapi.mapper.MedicineMapper;
import com.woniuxy.oldmanapi.mapper.WarehouseDetailMapper;
import com.woniuxy.oldmanapi.mapper.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class WareHouseService {
    @Autowired
    WarehouseMapper warehouseMapper;
    @Autowired
    WarehouseDetailMapper warehouseDetailMapper;
    @Autowired
    MedicineMapper medicineMapper;

    public PageInfo<WarehouseVo> findWarehouseVosByCons(String buyname, int state, int size, int now) {
        PageHelper.startPage(now,size);
        List<WarehouseVo> list=warehouseMapper.selectByCons(buyname,state);
        return  new PageInfo<>(list);
    }

    //入库==入库订单，入库明细添加
    @Transactional
    public void addWarehouse(WarehouseVo warehouseVo) {
        Warehouse warehouse=new Warehouse();
        warehouse.setBuyer(warehouseVo.getBuyer());
        warehouse.setDesc(warehouseVo.getDesc());
        String s = validateCode();
        warehouse.setOrdernum(s);
        warehouse.setState(0);
        warehouse.setTotalprice(warehouseVo.getTotalprice());
        warehouse.setBuytime("");
        warehouse.setCreatetime(warehouseVo.getCreatetime());

        warehouseMapper.insetWarehouse(warehouse);
        List<WarehouseDetailVo> warehouseDetailVos = warehouseVo.getWarehouseDetailVos();
        Integer warehouseId= warehouse.getId();
        Date date = warehouseVo.getCreatetime();
        warehouseDetailMapper.insertBeach(warehouseId,date,warehouseDetailVos);

    }

    //生成订单编号
    private String validateCode(){
        String data = "0123456789" ;
        String str = "WNwarehouse";
        Random random = new Random();
        for (int i=0;i<5;i++){
            //获取 0 --9 随机数
            int k = random.nextInt(10);
            str += data.charAt(k);
        }
        return str ;
    }

    //入库=订单修改，删除明细，添加明细
    @Transactional
    public void modifyWarehouse(WarehouseVo warehouseVo) {
        Warehouse warehouse=new Warehouse();
        warehouse.setBuyer(warehouseVo.getBuyer());
        warehouse.setDesc(warehouseVo.getDesc());
        warehouse.setOrdernum(warehouseVo.getOrdernum());
        warehouse.setState(warehouseVo.getState());
        warehouse.setTotalprice(warehouseVo.getTotalprice());
        warehouse.setBuytime(warehouseVo.getBuytime());
        warehouse.setCreatetime(warehouseVo.getCreatetime());
        warehouse.setId(warehouseVo.getId());
        warehouseMapper.updateWarehouse(warehouse);

        Integer warehouseId = warehouseVo.getId();
        warehouseDetailMapper.deleteByWarehouseId(warehouseId);
        List<WarehouseDetailVo> warehouseDetailVos = warehouseVo.getWarehouseDetailVos();
        Date date = warehouseVo.getCreatetime();
        warehouseDetailMapper.insertBeach(warehouseId,date,warehouseDetailVos);

    }

    //撤销
    public void cancelWarehouse(int id) {
        int state=2;
        warehouseMapper.updateWareseState(id,state);
    }
    //入库==库存，状态
    @Transactional
    public void putWarehouse(WarehouseVo warehouseVo) {
        Integer warehouseVoId = warehouseVo.getId();
        int  state=1;
        warehouseMapper.updateWareseState(warehouseVoId,state);
        List<WarehouseDetailVo> warehouseDetailVos = warehouseVo.getWarehouseDetailVos();
        for(WarehouseDetailVo w:warehouseDetailVos){
            Integer id1 = w.getMedicine().getId();
            Integer count = w.getCount();
            int i = medicineMapper.backaddScoreCount(id1, count);
        }
    }
}
